package com.example.demo.service.lmpl;

import com.example.demo.mapper.CategoryMapper;
import com.example.demo.pojo.Category;
import com.example.demo.service.CategoryService;
import com.example.demo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Service
public class CategoryServicelmpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //从Thread获得用户ID
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("id");
        category.setCreateUser(userId);
        //调用maaper,信息添加到数据库
        categoryMapper.add(category);


    }

    @Override
    public List<Category> list() {
        //查询当前用户所创建的分类信息，所以需要当前用户的ID，进行查询
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("id");
        //根据当前用户ID获取分类列表
        return categoryMapper.list(userId);
    }

    @Override
    public void update(Category category) {
        //更新时间，设置为当前时间
        category.setUpdateTime(LocalDateTime.now());
        //调用mapper
        categoryMapper.update(category);


    }

    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }
}
